package com.hkyp.ss.course.web.controller;

import com.hkyp.ss.course.apis.CourseCommentService;
import com.hkyp.ss.course.apis.UURelationService;
import com.hkyp.ss.course.apis.UserCourseRelationService;
import com.hkyp.ss.course.apis.UserService;
import com.hkyp.ss.course.apis.cons.CommonConstants;
import com.hkyp.ss.course.apis.dto.common.PageReq;
import com.hkyp.ss.course.apis.dto.common.PageResp;
import com.hkyp.ss.course.apis.dto.request.ModifyEmailPreDTO;
import com.hkyp.ss.course.apis.dto.request.PasswordDTO;
import com.hkyp.ss.course.apis.dto.request.ResetPwd2RequestDTO;
import com.hkyp.ss.course.apis.dto.response.*;
import com.hkyp.ss.course.apis.exception.UnacceptableException;
import com.hkyp.ss.course.apis.utils.BeanMapper;
import com.hkyp.ss.course.apis.utils.DataCheckUtil;
import com.hkyp.ss.course.web.dto.base.ResultBean;
import com.hkyp.ss.course.web.dto.request.WebPasswordDTO;
import com.hkyp.ss.course.web.dto.request.WebModifyEmailPreDTO;
import com.hkyp.ss.course.web.dto.request.WebResetPwd2DTO;
import com.hkyp.ss.course.web.utils.UserSessionUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@Api(description = "用户")
@Slf4j
@RestController
@RequestMapping(value = "/a/h/")
public class UserController {
    @Reference(version = "1.0")
    private UserService userService;
    @Reference(version = "1.0")
    private CourseCommentService couserCommentService;
    @Reference(version = "1.0")
    private UserCourseRelationService userCourseRelationService;

    @Reference(version = "1.0")
    private UURelationService uuRelationService;



    /**
     * 用户主页
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "用户主页-用户信息")
    @RequestMapping(value = "/api/user", method = RequestMethod.GET)
    public ResultBean<UserHomePageVO> userHomePage( ){
        long userId = UserSessionUtils.getUserId();
        return new ResultBean<>(userService.selectUserHomePage(userId));

    }

    /**
     * 用户主页-我的课评
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "用户主页-我的课评")
    @RequestMapping(value = "/api/user/homepage/coursecomment", method = RequestMethod.GET)
    public ResultBean<PageResp<UserHPCourCommVO>> userHomePageCourseComment(PageReq pageReq){
        long userId = UserSessionUtils.getUserId();
        //todo
        return new ResultBean<>( couserCommentService.selectCourseCommentByUserId(pageReq,userId,userId));

    }

    /**
     * 用户主页-课程收藏
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "用户主页-课程收藏")
    @RequestMapping(value = "/api/user/homepage/coursecollection", method = RequestMethod.GET)
    public ResultBean<PageResp<UserHPCourseVO>> userHomePageCourseCollection(PageReq pageReq ){
        long userId = UserSessionUtils.getUserId();
        return new ResultBean<>(userCourseRelationService.selectCourseByUserCollect(pageReq,userId));

    }

    /**
     * 用户主页-我的关注
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "用户主页-我的关注")
    @RequestMapping(value = "/api/user/following", method = RequestMethod.GET)
    public ResultBean<PageResp<SimpleUserVO>> userHomePageFollowing(PageReq pageReq ){
        long userId = UserSessionUtils.getUserId();
        return new ResultBean<>(uuRelationService.selectByFromId(pageReq,userId));

    }

    /**
     * 用户主页-我的粉丝
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "用户主页-我的粉丝")
    @RequestMapping(value = "/api/user/followers", method = RequestMethod.GET)
    public ResultBean<PageResp<SimpleUserVO>> userHomePageFollowers(PageReq pageReq ){
        long userId = UserSessionUtils.getUserId();
        return new ResultBean<>(uuRelationService.selectByToId(pageReq,userId));
    }
/*************************************************************************************/

    /**
     * 用户主页
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "用户主页-用户信息")
    @RequestMapping(value = "/api/user/{userId}", method = RequestMethod.GET)
    public ResultBean<UserHomePageVO> userHomePage(@PathVariable Long userId ){

      return new ResultBean<>(userService.selectUserHomePage(userId));

    }

    /**
     * 用户主页-我的课评
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "用户主页-我的课评")
    @RequestMapping(value = "/api/user/{userId}/homepage/coursecomment", method = RequestMethod.GET)
    public ResultBean<PageResp<UserHPCourCommVO>> userHomePageCourseComment(@PathVariable Long userId, PageReq pageReq){

        Long watcherUserId = UserSessionUtils.getUserId();
        //todo
        return new ResultBean<>( couserCommentService.selectCourseCommentByUserId(pageReq,userId,watcherUserId));

    }

    /**
     * 用户主页-课程收藏
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "用户主页-课程收藏")
    @RequestMapping(value = "/api/user/{userId}/homepage/coursecollection", method = RequestMethod.GET)
    public ResultBean<PageResp<UserHPCourseVO>> userHomePageCourseCollection(@PathVariable Long userId, PageReq pageReq ){

        return new ResultBean<>(userCourseRelationService.selectCourseByUserCollect(pageReq,userId));

    }

    /**
     * 用户主页-我的关注
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "用户主页-我的关注")
    @RequestMapping(value = "/api/user/{userId}/following", method = RequestMethod.GET)
    public ResultBean<PageResp<SimpleUserVO>> userHomePageFollowing(@PathVariable Long userId, PageReq pageReq ){

        return new ResultBean<>(uuRelationService.selectByFromId(pageReq,userId));

    }

    /**
     * 用户主页-我的粉丝
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "用户主页-我的粉丝")
    @RequestMapping(value = "/api/user/{userId}/followers", method = RequestMethod.GET)
    public ResultBean<PageResp<SimpleUserVO>> userHomePageFollowers(@PathVariable Long userId, PageReq pageReq ){

        return new ResultBean<>(uuRelationService.selectByToId(pageReq,userId));
    }

    /**
     * 用户 关注-取关
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "用户 关注-取关")
    @RequestMapping(value = "/api/user/uurelation", method = RequestMethod.POST)
    public ResultBean putUURelation(Long toUserId,Integer action){

        long userId = UserSessionUtils.getUserId();
        uuRelationService.putUURelation(userId,toUserId,action);
        return new ResultBean();

    }



    /********************************** 个人资料页 *************************************************/
    /**
     * 个人资料-基础信息
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "个人资料-基础信息-拉取")
    @RequestMapping(value = "/api/user/baseinfo", method = RequestMethod.GET)
    public ResultBean<UserBaseInfoDTO> getUserBaseInfo(){

        long userId = UserSessionUtils.getUserId();
        return new ResultBean<>(userService.getUserBaseInfo( userId));

    }

    /**
     * 个人资料-基础信息
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "个人资料-基础信息-更新")
    @RequestMapping(value = "/api/user/baseinfo", method = RequestMethod.PUT)
    public ResultBean postUserBaseInfo( @Valid @RequestBody UserBaseInfoDTO userBaseInfoDTO ){


        long userId = UserSessionUtils.getUserId();
        userService.postUserBaseInfo(userBaseInfoDTO,userId);
        return new ResultBean();

    }

    /**
     * 个人资料-头像设置-修改头像
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "个人资料-头像设置-修改头像")
    @RequestMapping(value = "/api/user/avatar", method = RequestMethod.PUT)
    public ResultBean putAvatar( @Valid @RequestBody UserAvatarRequestDTO userBaseInfoDTO ){


        long userId = UserSessionUtils.getUserId();
        userService.putUserAvatar(userBaseInfoDTO,userId);
        return new ResultBean();

    }

    /**
     * 个人资料-修改密码
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "个人资料-修改密码")
    @RequestMapping(value = "/api/user/pwd", method = RequestMethod.PUT)
    public ResultBean modifyPassword( @Valid @RequestBody WebPasswordDTO passwordDTO ){

        long userId = UserSessionUtils.getUserId();
        userService.modifyPassword(BeanMapper.map(passwordDTO, PasswordDTO.class),userId);
        return new ResultBean();

    }

    /**
     * 个人资料-修改邮箱-发送激活链接
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "个人资料-修改邮箱-发送激活链接")
    @RequestMapping(value = "/api/user/email", method = RequestMethod.PUT)
    public ResultBean modifyEmailPre(@RequestBody WebModifyEmailPreDTO webModifyEmailPreDTO, HttpServletRequest request){

        // 如果获取不到登录token或者token校验失败，会抛异常
        UserSessionUtils.getUserId();
        String loginToken = UserSessionUtils.getToken();



        DataCheckUtil.isMail(webModifyEmailPreDTO.getEmail());
        if (StringUtils.isEmpty(webModifyEmailPreDTO.getPassWord())) {
            throw new UnacceptableException("密码不能为空");
        }

        long userId = UserSessionUtils.getUserId();
        ModifyEmailPreDTO modifyEmailPreDTO = new ModifyEmailPreDTO();
        modifyEmailPreDTO.setBasePath(CommonConstants.WEB_REQUEST_URL);
        modifyEmailPreDTO.setEmail(webModifyEmailPreDTO.getEmail());
        modifyEmailPreDTO.setPassWord(webModifyEmailPreDTO.getPassWord());
        modifyEmailPreDTO.setUserId(userId);
        modifyEmailPreDTO.setLoginToken(loginToken);
        userService.modifyEmailPre(modifyEmailPreDTO);
        return new ResultBean();

    }




    /************************************* 上传头像   弃用 ********************************************************/
//    @ApiOperation(value = "上传图片")
//    @RequestMapping(value = "/u/upload", method = RequestMethod.POST)
//    public ResultBean getUpload(Model model, HttpServletRequest request,
//                                @RequestParam(value = "description",required=false) String description,
//                                @RequestParam("file") MultipartFile file)throws Exception
//    {
//        System.out.println(description);
//        long userId = UserSessionUtils.getUserId();
//
//        return new ResultBean<>(userService.upload(file,userId));
//    }
    /*******重置密码、、修改邮箱****/
    /**
     * 个人资料-修改邮箱
     * @return
     * @throws Exception
     */

    @ApiOperation(value = "个人资料-修改邮箱-接受激活请求")
    @RequestMapping(value = "/user/email/mo", method = RequestMethod.GET)
    public ResultBean modifyEmail(String token){

        if(StringUtils.isEmpty(token)){
            throw new UnacceptableException("token不能为空");
        }

            userService.modifyEmail(token);

        return new ResultBean<>();
    }
    /**
     * 重置密码-接受重置密码请求
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "重置密码-接受重置密码请求")
    @RequestMapping(value = "/user/pwd", method = RequestMethod.PUT)
    public ResultBean resetPwd(@Valid @RequestBody WebResetPwd2DTO webResetPwd2DTO, HttpServletRequest request){

        ResetPwd2RequestDTO resetPwdDTO = new ResetPwd2RequestDTO();
        resetPwdDTO.setPassword(webResetPwd2DTO.getPassword());
        resetPwdDTO.setToken(webResetPwd2DTO.getToken());

            userService.reSetPwdSendEmail(resetPwdDTO);
        return new ResultBean<>();
    }


}
